﻿using HandyControl.Controls;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using wms_wpf.Apiservice;
using wms_wpf.Dto;
using wms_wpf.InPut;
using wms_wpf.Views;
using Prism.Ioc;

namespace wms_wpf.ViewModels.ExpressChildViewModel
{
   public class CusetomOrderChildViewModel:BindableBase
    {

        string Machine = Environment.GetEnvironmentVariable("Machine", EnvironmentVariableTarget.Machine);

     
        public CusetomOrderChildViewModel()
        {

            this.dialogService = ContainerLocator.Container.Resolve<IDialogService>();
            viewService = new ViewService();
            baseService = new BaseService();
            dialogParameters = new DialogParameters();
            GetListCusetomCommand = new DelegateCommand(GetListCusetom);
            SelectionChangedCommand = new DelegateCommand(GetListCusetomLine);
            CreateTaskNeetOutCommand = new DelegateCommand(CreateTaskNeetOut);
            CreateTaskManyNeetOutCommand = new DelegateCommand(CreateTaskManyNeetOut);
            GetList_View_repestorderCommand = new DelegateCommand(GetList_View_repestorder);
            DeleteOrderCommand = new DelegateCommand(DeleteOrder);
            IndexCommand = new DelegateCommand<string>(Index);
            InPut_BaseService17 = new InPut_BaseService17();
            CusetomOrderDto = new CusetomOrderDto();
            CusetomOrderDtos = new List<CusetomOrderDto>();
            CusetomLineDtos = new List<CusetomLineDto>();
            GetListCusetom();
            GetList_View_repestorder();
        }


        private readonly IViewService viewService;
        private readonly IBaseService baseService;
        private readonly IDialogParameters dialogParameters;
        private readonly IDialogService dialogService;
        // 在你需要使用IDialogService的地方



        public DelegateCommand GetListCusetomCommand { get; set; }
        public DelegateCommand SelectionChangedCommand { get; set; }
        public DelegateCommand CreateTaskNeetOutCommand { get; set; }
        public DelegateCommand CreateTaskManyNeetOutCommand { get; set; }
        public DelegateCommand GetList_View_repestorderCommand { get; set; }
        public DelegateCommand DeleteOrderCommand { get; set; }
        public DelegateCommand<string> IndexCommand { get; set; }
        private List<CusetomOrderDto> cusetomOrderDtos;
        public List<CusetomOrderDto> CusetomOrderDtos
        {
            get { return cusetomOrderDtos; }
            set { cusetomOrderDtos = value; RaisePropertyChanged(); }
        }
        private List<CusetomLineDto> cusetomLineDtos;
        public List<CusetomLineDto> CusetomLineDtos
        {
            get { return cusetomLineDtos; }
            set { cusetomLineDtos = value; RaisePropertyChanged(); }
        }
        private List<View_View_repeatOrderDto> viewRepeatOrderDtos;
        public List<View_View_repeatOrderDto> View_View_RepeatOrderDtos
        {
            get { return viewRepeatOrderDtos; }
            set { viewRepeatOrderDtos = value; RaisePropertyChanged(); }
        }
        private int selectIndex;
        public int SelectIndex
        {
            get { return selectIndex; }
            set { selectIndex = value; RaisePropertyChanged(); }
        }

        private InPut_BaseService17 inPut_BaseService17;

        public InPut_BaseService17 InPut_BaseService17
        {
            get { return inPut_BaseService17; }
            set { inPut_BaseService17 = value; RaisePropertyChanged(); }
        }

        private CusetomOrderDto cusetomOrderDto;

        public CusetomOrderDto CusetomOrderDto
        {
            get { return cusetomOrderDto; }
            set { cusetomOrderDto = value; RaisePropertyChanged(); }
        }


        //删除工单
        private async void DeleteOrder()
        {
            if (CusetomOrderDtos.Count < 1)
            {
                MessageBox.Show("请选择工单");
                return;
            }
            List<Guid> guids = new List<Guid>();
            foreach (CusetomOrderDto item in CusetomOrderDtos)
            {

                guids.Add(item.CusetomOrder_Id);

            }
            bool res = await baseService.DeleteOrder(guids);
            if (res)
            {
                GetListCusetom();
                GetListCusetomLine();
            }
        }
        private async void GetListCusetom()
        {



            try
            {
                CusetomOrderDtos = await baseService.GetListCusetom(InPut_BaseService17);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("系统-GetListCusetom", ex);
            }


        }
        private async void GetListCusetomLine()
        {
            try
            {
                if (cusetomOrderDto == null) return;
                CusetomLineDtos = await baseService.GetListCusetomLine(cusetomOrderDto.CusetomOrder_Id);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("系统-GetListCusetomLine", ex);
            }

        }
        private async void CreateTaskNeetOut()
        {
            try
            {

                if (CusetomOrderDtos.Count > 1)
                {
                    Growl.Info("只能选择一个工单分配", "msg");
                    return;
                }

                if (cusetomOrderDto == null) return;
                InPut_BaseService20 inPut = new InPut_BaseService20();
                inPut.CusetomOrder_Id = cusetomOrderDto.CusetomOrder_Id;
                inPut.Machine = Machine;
                bool res = await baseService.CreateTaskNeetOut(inPut);
                if (res)
                {
                    GetListCusetom();
                    GetListCusetomLine();
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("系统-CreateTaskNeetOut", ex);
            }
        }
        private async void CreateTaskManyNeetOut()//合并工单
        {
            if (InPut_BaseService17.State == "已分配")
            {
                return;
            }
            if (CusetomLineDtos.Count > 8)
            {
                MessageBox.Show("最多只能合8个");
                return;
            }

            if (CusetomLineDtos.Count < 1)
            {
                MessageBox.Show("请选择工单");
                return;
            }
            DialogParameters dialogParameters = new DialogParameters();
            int c = 1;
            foreach (CusetomOrderDto item in CusetomOrderDtos)
            {
                // 添加每个项目的id和整个对象作为一个值（或其他你想单独提取的属性）

                dialogParameters.Add(c.ToString(), item.hSerial.ToString());
                c++;

            }

            dialogService.ShowDialog("DetailsDialog", dialogParameters, (r =>
            {
                var resulit = r.Result;
                if (resulit == ButtonResult.OK)
                {
                    GetListCusetom();
                    GetListCusetomLine();
                }
            }));
        }
        private void Index(string obj)
        {
            switch (obj)
            {
                case "TwoPage": SelectIndex = 1; GetListCusetom(); break;
                case "OnePage": SelectIndex = 0; ; break;

            }
        }
        private async void GetList_View_repestorder()
        {
            View_View_RepeatOrderDtos = await viewService.GetList_View_repestorder();
        }
    }
}

